<template>
	<view class="memo-container">
		<!-- 渐变背景 -->
		<view class="gradient-bg"></view>
		
		<!-- 标题栏 -->
		<view class="header-section">
			<text class="header-title">我的备忘录</text>
			<text class="header-subtitle">{{ dataList.length }}条记录</text>
		</view>
		
		<!-- 备忘录列表 -->
		<scroll-view class="memo-list" scroll-y="true" @scrolltolower="loadMore">
			<view class="memo-grid">
				<view 
					class="memo-card" 
					v-for="item in dataList" 
					:key="item.id"
					@click="itemClick(item)"
				>
					<view class="memo-content">
						<text class="memo-title">{{ item.title || '标题标题标题' }}</text>
						<text class="memo-desc">{{ item.desc || '内容内容内容内容内容内容内容内容内容内容内容内容...' }}</text>
						<text class="memo-time">{{ formatTime(item.add_time) }}</text>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="load-more" v-if="hasMore">
				<text class="load-text">正在加载更多...</text>
			</view>
			
			<!-- 底部占位 -->
			<view class="bottom-space"></view>
		</scroll-view>
		
		<!-- 悬浮添加按钮 -->
		<view class="fab-container" @click="addMemo">
			<view class="fab-button">
				<text class="fab-icon">+</text>
			</view>
		</view>
	</view>
</template>

<script>
import localStorage from '@/utils/storage.js'

export default {
	data() {
		return {
			dataList: [],
			hasMore: true,
			currentPage: 1,
			pageSize: 10
		}
	},
	onLoad(op) {
		// 先检查登录状态
		if (!this.checkLoginStatus()) {
			return; // 如果未登录则直接返回，不加载数据
		}
		this.loadData();
	},
	onShow() {
		// 检查登录状态
		if (!this.checkLoginStatus()) {
			return; // 如果未登录则直接返回，不刷新数据
		}
		// 从其他页面返回时刷新数据
		this.refreshData();
	},
	methods: {
		checkLoginStatus() {
			// 检查登录状态
			if (!localStorage.isLoggedIn()) {
				uni.showModal({
					title: '提示',
					content: '请先登录',
					showCancel: false,
					success: () => {
						uni.navigateTo({
							url: '/pages/public/login'
						});
					}
				});
				return false;
			}
			return true;
		},
		
		loadData() {
			// 使用本地存储获取备忘录列表
			const result = localStorage.getMemoList({
				page: this.currentPage,
				pageSize: this.pageSize
			});
			
			if (result.success) {
				if (this.currentPage === 1) {
					this.dataList = result.data.list.map(item => ({
						...item,
						desc: this.stripHtml(item.content).substring(0, 50) + '...',
						add_time: item.createTime
					}));
				} else {
					const newData = result.data.list.map(item => ({
						...item,
						desc: this.stripHtml(item.content).substring(0, 50) + '...',
						add_time: item.createTime
					}));
					this.dataList = [...this.dataList, ...newData];
				}
				this.hasMore = result.data.hasMore;
			} else {
				// 如果没有数据，显示空状态
				if (this.currentPage === 1) {
					this.dataList = [];
				}
				this.hasMore = false;
			}
		},
		
		// 去除HTML标签的工具函数
		stripHtml(html) {
			if (!html) return '';
			return html.replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').trim();
		},
		
		loadMore() {
			if (this.hasMore) {
				this.currentPage++;
				this.loadData();
			}
		},
		
		refreshData() {
			this.currentPage = 1;
			this.hasMore = true;
			this.loadData();
		},
		
		itemClick(item) {
			uni.navigateTo({
				url: '/pages/subPack/index/detail?id=' + item.id + '&title=' + encodeURIComponent(item.title || '备忘录详情')
			});
		},
		
		addMemo() {
			// 检查登录状态
			if (!this.checkLoginStatus()) {
				return;
			}
			
			// 跳转到编辑页面
			uni.navigateTo({
				url: '/pages/subPack/index/edit',
				fail: (err) => {
					uni.showToast({
						title: '页面跳转失败',
						icon: 'none'
					});
				}
			});
		},
		
		formatTime(time) {
			if (!time) return '';
			const date = new Date(time);
			const now = new Date();
			const diff = now - date;
			
			if (diff < 60000) return '刚刚';
			if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前';
			if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前';
			if (diff < 604800000) return Math.floor(diff / 86400000) + '天前';
			
			return date.toLocaleDateString();
		}
	}
}
</script>

<style lang="scss" scoped>
.memo-container {
	width: 100%;
	height: 100vh;
	position: relative;
	overflow: hidden;
}

.gradient-bg {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: linear-gradient(135deg, #a6c0fe 0%, #f68084 100%);
	z-index: 0;
}

.header-section {
	position: relative;
	z-index: 1;
	padding: 80rpx 40rpx 40rpx;
	text-align: center;
}

.header-title {
	display: block;
	font-size: 52rpx;
	font-weight: 700;
	color: #fff;
	margin-bottom: 10rpx;
	text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.header-subtitle {
	display: block;
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.8);
}

.memo-list {
	position: relative;
	z-index: 1;
	flex: 1;
	height: calc(100vh - 220rpx);
	padding: 0 30rpx;
}

.memo-grid {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	gap: 20rpx;
}

.memo-card {
	width: calc(50% - 10rpx);
	background: #ffffff;
	border-radius: 20rpx;
	padding: 25rpx;
	margin-bottom: 30rpx;
	box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
	transition: all 0.3s ease;
	box-sizing: border-box;
}

.memo-card:active {
	transform: translateY(2rpx);
	box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.15);
}

.memo-content {
	width: 100%;
}

.memo-title {
	display: block;
	font-size: 30rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 15rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.memo-desc {
	display: block;
	font-size: 24rpx;
	color: #666;
	line-height: 1.5;
	margin-bottom: 15rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

.memo-time {
	display: block;
	font-size: 20rpx;
	color: #999;
}

.load-more {
	text-align: center;
	padding: 40rpx 0;
}

.load-text {
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.7);
}

.bottom-space {
	height: 120rpx;
}

.fab-container {
	position: fixed;
	right: 20rpx;
	bottom: 100rpx;
	width: 150rpx;
	height: 150rpx;
	z-index: 9999;
	display: flex;
	align-items: center;
	justify-content: center;
	pointer-events: auto;
}

.fab-button {
	width: 110rpx;
	height: 110rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 55rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 12rpx 36rpx rgba(102, 126, 234, 0.4);
	transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
	border: 4rpx solid #ffffff;
}

.fab-container:active .fab-button {
	transform: scale(0.92);
	box-shadow: 0 8rpx 28rpx rgba(102, 126, 234, 0.35);
}

.fab-icon {
	font-size: 50rpx;
	color: #fff;
	font-weight: 200;
	line-height: 1;
	text-shadow: 0 1rpx 4rpx rgba(0, 0, 0, 0.2);
}
</style>